package com.lianniu.web.favorite.controller;

import com.lianniu.web.base.BaseResult;
import com.lianniu.web.favorite.model.FavoriteVo;
import com.lianniu.web.favorite.service.FavoriteService;
import com.lianniu.web.util.StringUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by Administrator on 2017-05-07.
 */
@Controller
@RequestMapping("/rest/favorite")
public class FavoriteRestController {
    Logger logger = LoggerFactory.getLogger(FavoriteRestController.class);
    @Autowired
    FavoriteService favoriteService;

    @RequestMapping("/addToFavorite")
    @ResponseBody
    public BaseResult addToFavorite(String userId,String skuId){
        logger.info("加入favorite，userid=[{}],skuId=[{}]",userId,skuId);
        if (StringUtils.isBlank(userId) ||
                StringUtils.isBlank(skuId)){
            return BaseResult.fail("0001","信息不全");
        }
        FavoriteVo favoriteVo = new FavoriteVo();
        favoriteVo.setcusUserId(userId);
        favoriteVo.setSupplyProductId(skuId);
        BaseResult baseResult = favoriteService.addToFavorite(favoriteVo);

        return baseResult;
    }

    @RequestMapping("isFavorite")
    @ResponseBody
    public BaseResult isFavorite(String userId,String skuId){
        logger.info("查看商品是否已经收藏，userid=[{}],skuId=[{}]", userId, skuId);
        if(StringUtils.isBlank(userId) || StringUtils.isBlank(skuId)){
            return BaseResult.fail("0001","信息不全");
        }
        FavoriteVo favoriteVo = new FavoriteVo();
        favoriteVo.setSupplyProductId(skuId);
        favoriteVo.setcusUserId(userId);
        BaseResult baseResult = favoriteService.isFavorite(favoriteVo);

        return baseResult;
    }

    @RequestMapping("getFavorites")
    @ResponseBody
    public BaseResult getFavorites(String userId,String pageNum)
    {
        if(StringUtils.isBlank(userId) ) {
            return BaseResult.fail("0001", "userId不能为空");
        }

        int page = 0 ;
        if(!StringUtils.isBlank(pageNum)) {
            page = Integer.valueOf(pageNum);
        }
        return favoriteService.getFavoritesProduct(userId, page);
    }
}

